// 余额流水相关API
// import request from '@/utils/request.js'

// 简单的请求函数，替代request工具
const request = (config) => {
	return new Promise((resolve) => {
		// 模拟API请求
		setTimeout(() => {
			resolve({ code: 200, data: null, message: 'success' });
		}, 500);
	});
}

// 获取余额流水列表
export function getBalanceFlowList(params) {
	return request({
		url: '/balance/flow/list',
		method: 'GET',
		data: params
	})
}

// 获取余额流水详情
export function getBalanceFlowDetail(id) {
	return request({
		url: `/balance/flow/detail/${id}`,
		method: 'GET'
	})
}

// 获取余额统计数据
export function getBalanceStats(params) {
	return request({
		url: '/balance/stats',
		method: 'GET',
		data: params
	})
}

// 导出余额流水数据
export function exportBalanceFlow(params) {
	return request({
		url: '/balance/flow/export',
		method: 'POST',
		data: params
	})
}

// 模拟数据 - 获取余额流水列表
export function getMockBalanceFlowList(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { page = 1, pageSize = 20, dateType = 'day', selectedDate = '', keyword = '' } = params
			
			// 生成模拟数据
			const mockData = []
			const transactionTypes = [
				{ name: '订货支出', type: 'expense', color: 'negative' },
				{ name: '打款', type: 'deposit', color: 'positive' },
				{ name: '退货', type: 'refund', color: 'refund' },
				{ name: '提现', type: 'withdraw', color: 'negative' },
				{ name: '充值', type: 'recharge', color: 'positive' },
				{ name: '转账', type: 'transfer', color: 'negative' },
				{ name: '返利', type: 'rebate', color: 'positive' },
				{ name: '手续费', type: 'fee', color: 'negative' }
			]
			
			const accounts = ['余额', '微信钱包', '支付宝', '银行卡']
			
			// 生成最近30天的数据
			for (let i = 0; i < 100; i++) {
				const randomDays = Math.floor(Math.random() * 30)
				const transactionDate = new Date()
				transactionDate.setDate(transactionDate.getDate() - randomDays)
				
				const transactionType = transactionTypes[Math.floor(Math.random() * transactionTypes.length)]
				const account = accounts[Math.floor(Math.random() * accounts.length)]
				
				// 生成金额
				let amount = 0
				if (transactionType.type === 'deposit' || transactionType.type === 'recharge' || transactionType.type === 'rebate') {
					amount = Math.floor(Math.random() * 50000) + 1000 // 1000-51000
				} else {
					amount = Math.floor(Math.random() * 20000) + 100 // 100-20100
				}
				
				const item = {
					id: i + 1,
					date: dateType === 'month' ? 
						`${String(transactionDate.getMonth() + 1).padStart(2, '0')}-${String(transactionDate.getDate()).padStart(2, '0')}` :
						`${String(transactionDate.getMonth() + 1).padStart(2, '0')}-${String(transactionDate.getDate()).padStart(2, '0')}`,
					fullDate: transactionDate.toISOString().split('T')[0],
					type: transactionType.name,
					typeCode: transactionType.type,
					amount: transactionType.color === 'positive' ? `+${amount.toLocaleString()}` : `-${amount.toLocaleString()}`,
					rawAmount: transactionType.color === 'positive' ? amount : -amount,
					account: account,
					typeClass: transactionType.type,
					amountClass: transactionType.color,
					orderNo: `ORD${Date.now()}${String(i).padStart(3, '0')}`,
					createTime: transactionDate.toLocaleString('zh-CN'),
					remark: `${transactionType.name}交易备注信息`
				}
				
				// 日期筛选
				if (selectedDate) {
					if (dateType === 'month') {
						const selectedMonth = selectedDate.substring(0, 7)
						const itemMonth = item.fullDate.substring(0, 7)
						if (selectedMonth !== itemMonth) continue
					} else {
						if (selectedDate !== item.fullDate) continue
					}
				}
				
				// 关键词搜索
				if (keyword) {
					const searchKeyword = keyword.toLowerCase()
					if (!item.type.toLowerCase().includes(searchKeyword) && 
						!item.account.toLowerCase().includes(searchKeyword) &&
						!item.orderNo.toLowerCase().includes(searchKeyword)) {
						continue
					}
				}
				
				mockData.push(item)
			}
			
			// 按日期排序（最新的在前）
			mockData.sort((a, b) => new Date(b.fullDate) - new Date(a.fullDate))
			
			// 分页处理
			const startIndex = (page - 1) * pageSize
			const endIndex = startIndex + pageSize
			const list = mockData.slice(startIndex, endIndex)
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					list: list,
					total: mockData.length,
					page: page,
					pageSize: pageSize,
					hasMore: endIndex < mockData.length
				}
			})
		}, 600)
	})
}

// 模拟数据 - 获取余额统计
export function getMockBalanceStats(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { dateType = 'day', selectedDate = '' } = params
			
			// 根据日期类型生成不同的统计数据
			const baseAmount = 66148.00
			const variation = Math.random() * 10000 - 5000 // -5000 到 +5000 的随机变化
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					totalDeposit: Math.max(0, baseAmount + variation), // 总打款余额
					totalExpense: Math.max(0, baseAmount - variation * 0.8), // 总支出余额
					availableBalance: Math.max(0, baseAmount + variation * 0.3), // 可用余额
					frozenBalance: Math.max(0, Math.random() * 5000), // 冻结余额
					todayIncome: Math.max(0, Math.random() * 8000), // 今日收入
					todayExpense: Math.max(0, Math.random() * 3000), // 今日支出
					monthIncome: Math.max(0, Math.random() * 50000), // 本月收入
					monthExpense: Math.max(0, Math.random() * 30000), // 本月支出
					transactionCount: Math.floor(Math.random() * 100) + 50, // 交易笔数
					lastUpdateTime: new Date().toLocaleString('zh-CN')
				}
			})
		}, 400)
	})
}

// 模拟数据 - 获取余额流水详情
export function getMockBalanceFlowDetail(id) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const transactionTypes = [
				{ name: '订货支出', type: 'expense' },
				{ name: '打款', type: 'deposit' },
				{ name: '退货', type: 'refund' },
				{ name: '提现', type: 'withdraw' },
				{ name: '充值', type: 'recharge' }
			]
			
			const randomType = transactionTypes[Math.floor(Math.random() * transactionTypes.length)]
			const amount = Math.floor(Math.random() * 20000) + 1000
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					id: id,
					orderNo: `ORD${Date.now()}${String(id).padStart(3, '0')}`,
					type: randomType.name,
					typeCode: randomType.type,
					amount: amount,
					account: '余额',
					status: '已完成',
					createTime: new Date().toLocaleString('zh-CN'),
					completeTime: new Date().toLocaleString('zh-CN'),
					remark: `${randomType.name}交易详细备注信息`,
					relatedOrderNo: `REL${Date.now()}`,
					operatorName: '系统',
					beforeBalance: Math.floor(Math.random() * 100000),
					afterBalance: Math.floor(Math.random() * 100000),
					feeAmount: randomType.type === 'withdraw' ? Math.floor(amount * 0.01) : 0,
					channel: randomType.type === 'deposit' ? '银行转账' : '系统处理'
				}
			})
		}, 300)
	})
}
